package com.zj.leetcode.easy.tree;

/**
 * @program: algorithm
 * @description: 二叉搜索树中的搜索
 * 700 search-in-a-binary-search-tree
 * @author: Zhang Bo
 * @create: 2022-07-05 10:27:08
 **/
public class SearchInABinarySearchTree {
    public static void main(String[] args) {
        Solution solution = new SearchInABinarySearchTree().new Solution();
    }
    //leetcode submit region begin(Prohibit modification and deletion)

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    class Solution {
        TreeNode sub = null;

        public TreeNode searchBST(TreeNode root, int val) {
            if (root == null) {
                return sub;
            }

            this.dfs(root, val);
            return sub;

        }


        private void dfs(TreeNode node, int val) {
            if (node != null) {
                if (node.val == val) {
                    sub = node;
                }
                if (node.left != null) {
                    this.dfs(node.left, val);
                }
                if(node.right != null){
                    this.dfs(node.right, val);
                }
            }
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}
